﻿@using System.Text.RegularExpressions

<MForm @bind-Value="valid">
    <MContainer>
        <MRow>
            <MCol Cols="12"
                  Md="4">
                <MTextField @bind-Value="firstName"
                            Counter="10"
                            Label="First name"
                            Required
                            Rules="@_nameRules">
                </MTextField>
            </MCol>

            <MCol Cols="12"
                  Md="4">
                <MTextField @bind-Value="lastName"
                            Counter="10"
                            Label="Last name"
                            Required
                            Rules="@_nameRules">
                </MTextField>
            </MCol>

            <MCol Cols="12"
                  Md="4">
                <MTextField @bind-Value="email"
                            Label="E-mail"
                            Required
                            Rules="@_emailRules">
                </MTextField>
            </MCol>
        </MRow>
    </MContainer>
</MForm>

@code {
    private static readonly Regex _emailRegex = new(".+@.+");

    private string? firstName;
    private string? lastName;
    private string? email;
    private bool valid;

    private List<Func<string?, StringBoolean>> _nameRules = new()
    {
        value => value?.Length <= 10 ? true : "Name must be less than 10 characters",
    };

    private List<Func<string?, StringBoolean>> _emailRules = new()
    {
        value => value != null && _emailRegex.Match(value).Success ? true : "E-mail must be valid",
    };

}